package database;

import java.util.ArrayList;

import data_model.DatabaseWeather;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHandler extends SQLiteOpenHelper {
	//Create DB
	public static final int DATABASE_VERSION=1;
	private static final String DATABASE_NAME="weatherDB.db";
	public static final String TABLE_CITY="city";
	public static final String TABLE_INFO="info";
	
	//Tao column cho bang City
	public static final String COLUMN_IDCITY="IDCity";
	public static final String COLUMN_CITY_NAME="Name";
	public static final String COLUMN_TIME_SET="time";
	
	//Tao column cho table Info
	//public static final String COLUMN_ID="CityID";
	public static final String COLUMN_THU_HT="ThuHT";
	public static final String COLUMN_NGAY_HT="NgayHT";
	public static final String COLUMN_THANG_HT="ThangHT";
	public static final String COLUMN_KIEUTROI_HT="KieutroiHT";
	public static final String COLUMN_NHIETDO_HT="NhietdoHT";
	public static final String COLUMN_NHIETDOMAX_HT="NhietDoMaxHT";
	public static final String COLUMN_NHIETDOMIN_HT="NhietDoMinHT";
	public static final String COLUMN_REAL_FEEL="RealFeel";
	public static final String COLUMN_DOAM_HT="DoAmHT";
	public static final String COLUMN_TOCDOGIO="TocDoGio";
	public static final String COLUMN_UV="UV";
	////--------ngay thu 2-----
	public static final String COLUMN_THU_2="Thu2";
	public static final String COLUMN_NGAY_2="Ngay2";
	public static final String COLUMN_THANG_2="Thang2";
	public static final String COLUMN_KIEUTROI_2="Kieutroi2";
	public static final String COLUMN_DOAM_2="Doam2";
	public static final String COLUMN_NHIETDOMAX_2="NhietDoMax2";
	public static final String COLUMN_NHIETDOMIN_2="NhietDoMin2";
	////--------ngay thu 3-----
	public static final String COLUMN_THU_3="Thu3";
	public static final String COLUMN_NGAY_3="Ngay3";
	public static final String COLUMN_THANG_3="Thang3";
	public static final String COLUMN_KIEUTROI_3="Kieutroi3";
	public static final String COLUMN_DOAM_3="Doam3";
	public static final String COLUMN_NHIETDOMAX_3="NhietDoMax3";
	public static final String COLUMN_NHIETDOMIN_3="NhietDoMin3";
	////--------ngay thu 4-----
	public static final String COLUMN_THU_4="Thu4";
	public static final String COLUMN_NGAY_4="Ngay4";
	public static final String COLUMN_THANG_4="Thang4";
	public static final String COLUMN_KIEUTROI_4="Kieutroi4";
	public static final String COLUMN_DOAM_4="Doam4";
	public static final String COLUMN_NHIETDOMAX_4="NhietDoMax4";
	public static final String COLUMN_NHIETDOMIN_4="NhietDoMin4";
	////--------ngay thu 5-----
	public static final String COLUMN_THU_5="Thu5";
	public static final String COLUMN_NGAY_5="Ngay5";
	public static final String COLUMN_THANG_5="Thang5";
	public static final String COLUMN_KIEUTROI_5="Kieutroi5";
	public static final String COLUMN_DOAM_5="Doam5";
	public static final String COLUMN_NHIETDOMAX_5="NhietDoMax5";
	public static final String COLUMN_NHIETDOMIN_5="NhietDoMin5";
	////--------ngay thu 6-----
	public static final String COLUMN_THU_6="Thu6";
	public static final String COLUMN_NGAY_6="Ngay6";
	public static final String COLUMN_THANG_6="Thang6";
	public static final String COLUMN_KIEUTROI_6="Kieutroi6";
	public static final String COLUMN_DOAM_6="Doam6";
	public static final String COLUMN_NHIETDOMAX_6="NhietDoMax6";
	public static final String COLUMN_NHIETDOMIN_6="NhietDoMin6";
	////--------ngay thu 7-----
	public static final String COLUMN_THU_7="Thu7";
	public static final String COLUMN_NGAY_7="Ngay7";
	public static final String COLUMN_THANG_7="Thang7";
	public static final String COLUMN_KIEUTROI_7="Kieutroi7";
	public static final String COLUMN_DOAM_7="Doam7";
	public static final String COLUMN_NHIETDOMAX_7="NhietDoMax7";
	public static final String COLUMN_NHIETDOMIN_7="NhietDoMin7";
	
	

	public MyDBHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
			String CREATE_TABLE_CITY="CREATE TABLE " + TABLE_CITY+ "(" 
					+ COLUMN_CITY_NAME + " TEXT PRIMARY KEY, "
					+ COLUMN_TIME_SET + " TEXT ,"
					+ COLUMN_THU_HT + " TEXT ,"
					+ COLUMN_NGAY_HT + " TEXT ,"
					+ COLUMN_THANG_HT + " TEXT ,"
					+ COLUMN_NHIETDO_HT+ " TEXT ,"
					+ COLUMN_NHIETDOMAX_HT + " TEXT ,"
					+ COLUMN_NHIETDOMIN_HT + " TEXT ,"
					+ COLUMN_KIEUTROI_HT + " TEXT ,"
					+ COLUMN_REAL_FEEL + " TEXT ,"
					+ COLUMN_DOAM_HT + " TEXT ,"
					+ COLUMN_TOCDOGIO + " TEXT ,"
					+ COLUMN_UV + " TEXT ,"
					+ COLUMN_THU_2 + " TEXT ,"
					+ COLUMN_NGAY_2 + " TEXT ,"
					+ COLUMN_THANG_2+ " TEXT ,"
					+ COLUMN_KIEUTROI_2 + " TEXT ,"
					+ COLUMN_NHIETDOMAX_2 + " TEXT ,"
					+ COLUMN_NHIETDOMIN_2 + " TEXT ,"
					+ COLUMN_DOAM_2 + " TEXT ,"
					+ COLUMN_THU_3 + " TEXT ,"
					+ COLUMN_NGAY_3 + " TEXT ,"
					+ COLUMN_THANG_3+ " TEXT ,"
					+ COLUMN_KIEUTROI_3 + " TEXT ,"
					+ COLUMN_NHIETDOMAX_3 + " TEXT ,"
					+ COLUMN_NHIETDOMIN_3 + " TEXT ,"
					+ COLUMN_DOAM_3 + " TEXT ,"
					+ COLUMN_THU_4 + " TEXT ,"
					+ COLUMN_NGAY_4 + " TEXT ,"
					+ COLUMN_THANG_4+ " TEXT ,"
					+ COLUMN_KIEUTROI_4 + " TEXT ,"
					+ COLUMN_NHIETDOMAX_4 + " TEXT ,"
					+ COLUMN_NHIETDOMIN_4 + " TEXT ,"
					+ COLUMN_DOAM_4 + " TEXT ,"
					+ COLUMN_THU_5 + " TEXT ,"
					+ COLUMN_NGAY_5 + " TEXT ,"
					+ COLUMN_THANG_5+ " TEXT ,"
					+ COLUMN_KIEUTROI_5 + " TEXT ,"
					+ COLUMN_NHIETDOMAX_5 + " TEXT ,"
					+ COLUMN_NHIETDOMIN_5 + " TEXT ,"
					+ COLUMN_DOAM_5 + " TEXT ,"
					+ COLUMN_THU_6 + " TEXT ,"
					+ COLUMN_NGAY_6 + " TEXT ,"
					+ COLUMN_THANG_6+ " TEXT ,"
					+ COLUMN_KIEUTROI_6 + " TEXT ,"
					+ COLUMN_NHIETDOMAX_6 + " TEXT ,"
					+ COLUMN_NHIETDOMIN_6+ " TEXT ,"
					+ COLUMN_DOAM_6 + " TEXT ,"
					+ COLUMN_THU_7 + " TEXT ,"
					+ COLUMN_NGAY_7 + " TEXT ,"
					+ COLUMN_THANG_7+ " TEXT ,"
					+ COLUMN_KIEUTROI_7 + " TEXT ,"
					+ COLUMN_NHIETDOMAX_7 + " TEXT ,"
					+ COLUMN_NHIETDOMIN_7+ " TEXT ,"
					+ COLUMN_DOAM_7 + " TEXT ,"
					+ COLUMN_IDCITY+ " TEXT )";
					

			db.execSQL(CREATE_TABLE_CITY);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS "+TABLE_CITY);
		onCreate(db);
	}

	public void addCity(DatabaseWeather dataWea){
		ContentValues values=new ContentValues();
		
		values.put(COLUMN_CITY_NAME, dataWea.get_city());
		values.put(COLUMN_TIME_SET, dataWea.get_timer());
		values.put(COLUMN_THU_HT, dataWea.get_thuht());
		values.put(COLUMN_NGAY_HT, dataWea.get_ngayht());
		values.put(COLUMN_THANG_HT, dataWea.get_thanght());
		values.put(COLUMN_NHIETDO_HT, dataWea.get_nhietdoht());
		values.put(COLUMN_NHIETDOMAX_HT, dataWea.get_nhietdocaonhat_1());
		values.put(COLUMN_NHIETDOMIN_HT, dataWea.get_nhietdothapnhat_1());
		values.put(COLUMN_KIEUTROI_HT, dataWea.get_kieutroi_1());
		values.put(COLUMN_REAL_FEEL, dataWea.getRealFeel());
		values.put(COLUMN_DOAM_HT, dataWea.get_doam());
		values.put(COLUMN_TOCDOGIO, dataWea.get_tocdogio());
		values.put(COLUMN_UV, dataWea.get_uv());
		//------------------------------------
		values.put(COLUMN_THU_2, dataWea.get_thu_2());
		values.put(COLUMN_NGAY_2, dataWea.get_ngay_2());
		values.put(COLUMN_THANG_2, dataWea.get_thang_2());
		values.put(COLUMN_KIEUTROI_2, dataWea.get_kieutroi_2());
		values.put(COLUMN_NHIETDOMAX_2, dataWea.get_nhietdocaonhat_2());
		values.put(COLUMN_NHIETDOMIN_2, dataWea.get_nhietdothapnhat_2());
		values.put(COLUMN_DOAM_2, dataWea.get_doam_2());
		//-------------------------------------
		values.put(COLUMN_THU_3, dataWea.get_thu_3());
		values.put(COLUMN_NGAY_3, dataWea.get_ngay_3());
		values.put(COLUMN_THANG_3, dataWea.get_thang_3());
		values.put(COLUMN_KIEUTROI_3, dataWea.get_kieutroi_3());
		values.put(COLUMN_NHIETDOMAX_3, dataWea.get_nhietdocaonhat_3());
		values.put(COLUMN_NHIETDOMIN_3, dataWea.get_nhietdothapnhat_3());
		values.put(COLUMN_DOAM_3, dataWea.get_doam_3());
		//---------------------------------------
		values.put(COLUMN_THU_4, dataWea.get_thu_4());
		values.put(COLUMN_NGAY_4, dataWea.get_ngay_4());
		values.put(COLUMN_THANG_4, dataWea.get_thang_4());
		values.put(COLUMN_KIEUTROI_4, dataWea.get_kieutroi_4());
		values.put(COLUMN_NHIETDOMAX_4, dataWea.get_nhietdocaonhat_4());
		values.put(COLUMN_NHIETDOMIN_4, dataWea.get_nhietdothapnhat_4());
		values.put(COLUMN_DOAM_4, dataWea.get_doam_4());
		//---------------------------------------
		values.put(COLUMN_THU_5, dataWea.get_thu_5());
		values.put(COLUMN_NGAY_5, dataWea.get_ngay_5());
		values.put(COLUMN_THANG_5, dataWea.get_thang_5());
		values.put(COLUMN_KIEUTROI_5, dataWea.get_kieutroi_5());
		values.put(COLUMN_NHIETDOMAX_5, dataWea.get_nhietdocaonhat_5());
		values.put(COLUMN_NHIETDOMIN_5, dataWea.get_nhietdothapnhat_5());
		values.put(COLUMN_DOAM_5, dataWea.get_doam_5());
		//---------------------------------------
		values.put(COLUMN_THU_6, dataWea.get_thu_6());
		values.put(COLUMN_NGAY_6, dataWea.get_ngay_6());
		values.put(COLUMN_THANG_6, dataWea.get_thang_6());
		values.put(COLUMN_KIEUTROI_6, dataWea.get_kieutroi_6());
		values.put(COLUMN_NHIETDOMAX_6, dataWea.get_nhietdocaonhat_6());
		values.put(COLUMN_NHIETDOMIN_6, dataWea.get_nhietdothapnhat_6());
		values.put(COLUMN_DOAM_6, dataWea.get_doam_6());
		//---------------------------------------
		values.put(COLUMN_THU_7, dataWea.get_thu_7());
		values.put(COLUMN_NGAY_7, dataWea.get_ngay_7());
		values.put(COLUMN_THANG_7, dataWea.get_thang_7());
		values.put(COLUMN_KIEUTROI_7, dataWea.get_kieutroi_7());
		values.put(COLUMN_NHIETDOMAX_7, dataWea.get_nhietdocaonhat_7());
		values.put(COLUMN_NHIETDOMIN_7, dataWea.get_nhietdothapnhat_7());
		values.put(COLUMN_DOAM_7, dataWea.get_doam_7());
		//---------------------------------------
		values.put(COLUMN_IDCITY, dataWea.get_cityCode());
		
		SQLiteDatabase db=this.getWritableDatabase();
		db.insert(TABLE_CITY, null, values);
		this.close();
	}
	
	public ArrayList<DatabaseWeather> findCity(){
		String sql="SELECT * FROM "+TABLE_CITY +" ORDER BY "+ COLUMN_TIME_SET+" DESC  LIMIT 0,1";
		SQLiteDatabase db=this.getWritableDatabase();
		
		Cursor cursor=db.rawQuery(sql, null);
		ArrayList<DatabaseWeather> arrWea=new ArrayList<DatabaseWeather>();
		
		if(cursor.moveToFirst()){
			do{
				DatabaseWeather dbWea=new DatabaseWeather();
				dbWea.set_city(cursor.getString(0));
				dbWea.set_timer(cursor.getString(1));
				dbWea.set_thuht(cursor.getString(2));
				dbWea.set_ngayht(cursor.getString(3));
				dbWea.set_thanght(cursor.getString(4));
				dbWea.set_nhietdoht(cursor.getString(5));
				dbWea.set_nhietdocaonhat_1(cursor.getString(6));
				dbWea.set_nhietdothapnhat_1(cursor.getString(7));
				dbWea.set_kieutroi_1(cursor.getString(8));
				dbWea.setRealFeel(cursor.getString(9));
				dbWea.set_doam(cursor.getString(10));
				dbWea.set_tocdogio(cursor.getString(11));
				dbWea.set_uv(cursor.getString(12));
				//--------------------------------------
				dbWea.set_thu_2(cursor.getString(13));
				dbWea.set_ngay_2(cursor.getString(14));
				dbWea.set_thang_2(cursor.getString(15));
				dbWea.set_kieutroi_2(cursor.getString(16));
				dbWea.set_nhietdocaonhat_2(cursor.getString(17));
				dbWea.set_nhietdothapnhat_2(cursor.getString(18));
				dbWea.set_doam_2(cursor.getString(19));
				//------------------------------------
				dbWea.set_thu_3(cursor.getString(20));
				dbWea.set_ngay_3(cursor.getString(21));
				dbWea.set_thang_3(cursor.getString(22));
				dbWea.set_kieutroi_3(cursor.getString(23));
				dbWea.set_nhietdocaonhat_3(cursor.getString(24));
				dbWea.set_nhietdothapnhat_3(cursor.getString(25));
				dbWea.set_doam_3(cursor.getString(26));
				//------------------------------------
				dbWea.set_thu_4(cursor.getString(27));
				dbWea.set_ngay_4(cursor.getString(28));
				dbWea.set_thang_4(cursor.getString(29));
				dbWea.set_kieutroi_4(cursor.getString(30));
				dbWea.set_nhietdocaonhat_4(cursor.getString(31));
				dbWea.set_nhietdothapnhat_4(cursor.getString(32));
				dbWea.set_doam_4(cursor.getString(33));
				//------------------------------------
				dbWea.set_thu_5(cursor.getString(34));
				dbWea.set_ngay_5(cursor.getString(35));
				dbWea.set_thang_5(cursor.getString(36));
				dbWea.set_kieutroi_5(cursor.getString(37));
				dbWea.set_nhietdocaonhat_5(cursor.getString(38));
				dbWea.set_nhietdothapnhat_5(cursor.getString(39));
				dbWea.set_doam_5(cursor.getString(40));
				//------------------------------------
				dbWea.set_thu_6(cursor.getString(41));
				dbWea.set_ngay_6(cursor.getString(42));
				dbWea.set_thang_6(cursor.getString(43));
				dbWea.set_kieutroi_6(cursor.getString(44));
				dbWea.set_nhietdocaonhat_6(cursor.getString(45));
				dbWea.set_nhietdothapnhat_6(cursor.getString(46));
				dbWea.set_doam_6(cursor.getString(47));
				//------------------------------------
				dbWea.set_thu_7(cursor.getString(48));
				dbWea.set_ngay_7(cursor.getString(49));
				dbWea.set_thang_7(cursor.getString(50));
				dbWea.set_kieutroi_7(cursor.getString(51));
				dbWea.set_nhietdocaonhat_7(cursor.getString(52));
				dbWea.set_nhietdothapnhat_7(cursor.getString(53));
				dbWea.set_doam_7(cursor.getString(54));
				//------------------------------------
				dbWea.set_cityCode(cursor.getString(55));
				
				arrWea.add(dbWea);
			}
			while(cursor.moveToNext());
		}
		cursor.close();
		this.close();
		return arrWea;
	}
	public boolean isDatabaseItemsExist(){
		SQLiteDatabase db=this.getWritableDatabase();
		boolean dbItemsExist=false;
		try{
			String sql="SELECT * FROM "+ TABLE_CITY;
			Cursor cursor=db.rawQuery(sql, null);
			if(cursor.getCount()!=0){
				dbItemsExist=true;
			}
		}
		catch(Exception e){
			
		}
		return dbItemsExist;
	}
	
	public void deleteCity(){
		String sql="DELETE FROM "+TABLE_CITY;
		SQLiteDatabase db=this.getWritableDatabase();
		db.execSQL(sql);
	    db.close();
	}
	
	public void updateCity(DatabaseWeather dataWea){
		SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        
        values.put(COLUMN_CITY_NAME, dataWea.get_city());
		values.put(COLUMN_TIME_SET, dataWea.get_timer());
		values.put(COLUMN_THU_HT, dataWea.get_thuht());
		values.put(COLUMN_NGAY_HT, dataWea.get_ngayht());
		values.put(COLUMN_THANG_HT, dataWea.get_thanght());
		values.put(COLUMN_NHIETDO_HT, dataWea.get_nhietdoht());
		values.put(COLUMN_NHIETDOMAX_HT, dataWea.get_nhietdocaonhat_1());
		values.put(COLUMN_NHIETDOMIN_HT, dataWea.get_nhietdothapnhat_1());
		values.put(COLUMN_KIEUTROI_HT, dataWea.get_kieutroi_1());
		values.put(COLUMN_REAL_FEEL, dataWea.getRealFeel());
		values.put(COLUMN_DOAM_HT, dataWea.get_doam());
		values.put(COLUMN_TOCDOGIO, dataWea.get_tocdogio());
		values.put(COLUMN_UV, dataWea.get_uv());
		//------------------------------------
		values.put(COLUMN_THU_2, dataWea.get_thu_2());
		values.put(COLUMN_NGAY_2, dataWea.get_ngay_2());
		values.put(COLUMN_THANG_2, dataWea.get_thang_2());
		values.put(COLUMN_KIEUTROI_2, dataWea.get_kieutroi_2());
		values.put(COLUMN_NHIETDOMAX_2, dataWea.get_nhietdocaonhat_2());
		values.put(COLUMN_NHIETDOMIN_2, dataWea.get_nhietdothapnhat_2());
		values.put(COLUMN_DOAM_2, dataWea.get_doam_2());
		//-------------------------------------
		values.put(COLUMN_THU_3, dataWea.get_thu_3());
		values.put(COLUMN_NGAY_3, dataWea.get_ngay_3());
		values.put(COLUMN_THANG_3, dataWea.get_thang_3());
		values.put(COLUMN_KIEUTROI_3, dataWea.get_kieutroi_3());
		values.put(COLUMN_NHIETDOMAX_3, dataWea.get_nhietdocaonhat_3());
		values.put(COLUMN_NHIETDOMIN_3, dataWea.get_nhietdothapnhat_3());
		values.put(COLUMN_DOAM_3, dataWea.get_doam_3());
		//---------------------------------------
		values.put(COLUMN_THU_4, dataWea.get_thu_4());
		values.put(COLUMN_NGAY_4, dataWea.get_ngay_4());
		values.put(COLUMN_THANG_4, dataWea.get_thang_4());
		values.put(COLUMN_KIEUTROI_4, dataWea.get_kieutroi_4());
		values.put(COLUMN_NHIETDOMAX_4, dataWea.get_nhietdocaonhat_4());
		values.put(COLUMN_NHIETDOMIN_4, dataWea.get_nhietdothapnhat_4());
		values.put(COLUMN_DOAM_4, dataWea.get_doam_4());
		//---------------------------------------
		values.put(COLUMN_THU_5, dataWea.get_thu_5());
		values.put(COLUMN_NGAY_5, dataWea.get_ngay_5());
		values.put(COLUMN_THANG_5, dataWea.get_thang_5());
		values.put(COLUMN_KIEUTROI_5, dataWea.get_kieutroi_5());
		values.put(COLUMN_NHIETDOMAX_5, dataWea.get_nhietdocaonhat_5());
		values.put(COLUMN_NHIETDOMIN_5, dataWea.get_nhietdothapnhat_5());
		values.put(COLUMN_DOAM_5, dataWea.get_doam_5());
		//---------------------------------------
		values.put(COLUMN_THU_6, dataWea.get_thu_6());
		values.put(COLUMN_NGAY_6, dataWea.get_ngay_6());
		values.put(COLUMN_THANG_6, dataWea.get_thang_6());
		values.put(COLUMN_KIEUTROI_6, dataWea.get_kieutroi_6());
		values.put(COLUMN_NHIETDOMAX_6, dataWea.get_nhietdocaonhat_6());
		values.put(COLUMN_NHIETDOMIN_6, dataWea.get_nhietdothapnhat_6());
		values.put(COLUMN_DOAM_6, dataWea.get_doam_6());
		//---------------------------------------
		values.put(COLUMN_THU_7, dataWea.get_thu_7());
		values.put(COLUMN_NGAY_7, dataWea.get_ngay_7());
		values.put(COLUMN_THANG_7, dataWea.get_thang_7());
		values.put(COLUMN_KIEUTROI_7, dataWea.get_kieutroi_7());
		values.put(COLUMN_NHIETDOMAX_7, dataWea.get_nhietdocaonhat_7());
		values.put(COLUMN_NHIETDOMIN_7, dataWea.get_nhietdothapnhat_7());
		values.put(COLUMN_DOAM_7, dataWea.get_doam_7());
		//---------------------------------------
		db.update(TABLE_CITY, values, COLUMN_CITY_NAME+"=?", new String[]{String.valueOf(dataWea.get_city())});
		
	}
}
